<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%
    String _visitId = input.getDesc("visit_id", "-1");
    List<Visit> list = QBuilder.db("basedb").queryHql("from Visit where id = " + _visitId);
    if (isEmpty(list)) {
        msg = new Message(30041, "问卷异常");
        Tools.encryptPrint(out, msg);
        return;
    }
    Visit v = list.get(0);
    Map<String, Object> surveyMap = new HashMap<String, Object>();
    int surveyId = v.getSurveyId();
    int visitId = v.getId();
    List<Survey> surveys = QBuilder.db("basedb").queryHql("from Survey where id = " + surveyId);

    Survey survey = surveys.get(0);
    String title = survey.getTitle();
    String date = v.getCheckTime();
    int id = survey.getId();
    surveyMap.put("title", title);
    surveyMap.put("id", id);
    surveyMap.put("date", date);
    surveyMap.put("isconfirmed", v.getIsconfirmed());
    // 查询副标题
    List<SurveyQuestion> questList = QBuilder.db("basedb").queryHql("from SurveyQuestion where status = " + STATUS_ENABLE + " and surveyId = "+ id + " order by showOrder asc");
    Iterator<SurveyQuestion> it = questList.iterator();
    List<SurveyQuestion> titles = new ArrayList<SurveyQuestion>();
    List<SurveyQuestion> mulInputs = new ArrayList<SurveyQuestion>();
    String questIds = "-1";
    while (it.hasNext()) {
        SurveyQuestion sq = it.next();
        if (sq.getType() == SURVEY_QUESTION_TYPE_PARENT) {
            // 副标题
            titles.add(sq);
            it.remove();
        } else if (sq.getType() == SURVEY_QUESTION_TYPE_MUTILINPUT) {
            // 多重填空的题目
            mulInputs.add(sq);
            it.remove();
        } else {
            // 问题
            questIds += ("," + sq.getId());
        }
    }
    // 根据问题id查出问题选项
    Map<Integer, List<Map<String, Object>>> questIdMap = new HashMap<Integer, List<Map<String, Object>>>(); // 问题和问题选项集合
    List<SurveyOptions> optionList = QBuilder.db("basedb").queryHql("from SurveyOptions where status = " + STATUS_ENABLE + " and questId in(" + questIds + ") order by showOrder asc");
    // 查出病人的答案
    List<SurveyAnswer> answerList = QBuilder.db("basedb").queryHql("from SurveyAnswer where status = " + STATUS_ENABLE + " and questId in(" + questIds + ") and visitId = " + visitId + " and surveyId = " + surveyId);
    for (int i = 0; i < optionList.size(); i++) {
        SurveyOptions option = optionList.get(i);
        String content = option.getContent();
        Integer optionId = option.getId();
        Integer questId = option.getQuestId();
        List<Map<String, Object>> options = questIdMap.get(questId);
        if (null == options) {
            options = new ArrayList<Map<String, Object>>();
        }
        Map<String, Object> anOption = new HashMap<String, Object>();
        anOption.put("content", content);
        anOption.put("option_id", optionId);
        Iterator<SurveyAnswer> itor = answerList.iterator();
        while (itor.hasNext()) {
            SurveyAnswer sa = itor.next();
            int type = sa.getType();
            Integer quest_id = sa.getQuestId();
            if (questId != null && questId.equals(quest_id)) {
                if (type == SURVEY_QUESTION_TYPE_INPUT) { // 填空题
                    anOption.put("answer", sa.getContent());
                    itor.remove();
                } else if (type == SURVEY_QUESTION_TYPE_MULTI || type == SURVEY_QUESTION_TYPE_SINGLE) {
                    Integer option_id = Tools.getInt(sa.getContent(), -99999);
                    if (option_id.equals(optionId)) {
                        anOption.put("answer", "1");
                        itor.remove();
                    }
                }
            }
        }
        options.add(anOption);
        questIdMap.put(questId, options);
    }
    // 问题选项加到对应的问题中
    LinkedList<Map<String, Object>> quests = new LinkedList<Map<String, Object>>(); // 问题列表
    for (int i = 0; i < questList.size(); i++) {
        SurveyQuestion quest = questList.get(i);
        String questTitle = quest.getTitle();
        Integer questId = quest.getId();
        Integer type = quest.getType();
        Integer parentId = quest.getParentId();
        Integer necessary = quest.getNecessary();
        Map<String, Object> aQuest = new HashMap<String, Object>();
        String json = Tools.getDesc(quest.getJsonString(), "{}");
        Map<String, String> jsonString = JSONUtil.fromJson(json, new TypeReference<Map>(){});
        aQuest.put("icon", jsonString.get("icon"));
        aQuest.put("title", questTitle);
        aQuest.put("quest_id", questId);
        aQuest.put("type", type);
        if (type == SURVEY_QUESTION_TYPE_MULTI || type == SURVEY_QUESTION_TYPE_SINGLE) {
            List<Map<String, Object>> tempList = questIdMap.get(questId);
            for (int j = 0; j < tempList.size(); j++) {
                Map<String, Object> tempMap = tempList.get(j);
                if (tempMap.get("answer") == null) {
                    tempMap.put("answer", "0");
                }
            }
        }
        aQuest.put("options", questIdMap.get(questId));
        aQuest.put("parent_id", parentId);
        aQuest.put("necessary", necessary);
        aQuest.put("show_order", quest.getShowOrder());
        quests.add(aQuest);
    }
    // 把多重填空子题目放入大标题中
    LinkedList<Map<String, Object>> dest = new LinkedList<Map<String, Object>>();
    dest.addAll(quests);
    for (int i = 0; i < mulInputs.size(); i++) {
        SurveyQuestion sq = mulInputs.get(i);
        String questTitle = sq.getTitle();
        Integer questId = sq.getId();
        Integer type = sq.getType();
        Integer parentId = sq.getParentId();
        Integer necessary = sq.getNecessary();
        Map<String, Object> aQuest = new HashMap<String, Object>();
        String json = Tools.getDesc(sq.getJsonString(), "{}");
        Map<String, String> jsonString = JSONUtil.fromJson(json, new TypeReference<Map>(){});
        aQuest.put("icon", jsonString.get("icon"));
        aQuest.put("title", questTitle);
        aQuest.put("quest_id", questId);
        aQuest.put("type", type);
        aQuest.put("parent_id", parentId);
        aQuest.put("necessary", necessary);
        aQuest.put("show_order", sq.getShowOrder());
        dest.addFirst(aQuest);
        Iterator<Map<String, Object>> itor = quests.iterator();
        while (itor.hasNext()) {
            Map<String, Object> map = itor.next();
            Integer sqId = (Integer)map.get("parent_id");
            if (questId.equals(sqId)) {
                List<Map<String, Object>> childList = (List<Map<String, Object>>)aQuest.get("multils");
                if (childList == null) {
                    childList = new ArrayList<Map<String, Object>>();
                    aQuest.put("multils", childList);
                }
                childList.add(map);
                itor.remove();
                dest.remove(map);
            }
        }
    }
    // 根据showorder排序
    quests.clear();
    quests.addAll(dest);
    Collections.sort(quests, new Comparator<Map<String, Object>>() {

        @Override
        public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            return Tools.getInt(o1.get("show_order") + "", 0).compareTo(Tools.getInt(o2.get("show_order")+"", 0));
        }
    });

    // 问题加入大分类
    dest.clear();
    dest.addAll(quests);
    for (int i = 0; i < titles.size(); i++) {
        SurveyQuestion sq = titles.get(i);
        String questTitle = sq.getTitle();
        Integer questId = sq.getId();
        Integer type = sq.getType();
        Integer parentId = sq.getParentId();
        Integer necessary = sq.getNecessary();
        Map<String, Object> aQuest = new HashMap<String, Object>();
        aQuest.put("title", questTitle);
        aQuest.put("quest_id", questId);
        aQuest.put("type", type);
        aQuest.put("parent_id", parentId);
        aQuest.put("necessary", necessary);
        aQuest.put("show_order", sq.getShowOrder());
        dest.addFirst(aQuest);
        Iterator<Map<String, Object>> itor = quests.iterator();
        while (itor.hasNext()) {
            Map<String, Object> map = itor.next();
            Integer sqId = (Integer)map.get("parent_id");
            if (questId.equals(sqId)) {
                List<Map<String, Object>> childList = (List<Map<String, Object>>)aQuest.get("quests");
                if (childList == null) {
                    childList = new ArrayList<Map<String, Object>>();
                    aQuest.put("quests", childList);
                }
                childList.add(map);
                itor.remove();
                dest.remove(map);
            }
        }
    }
    Collections.sort(dest, new Comparator<Map<String, Object>>() {

        @Override
        public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            return Tools.getDesc(o1.get("show_order"), "0").compareTo(Tools.getDesc(o2.get("show_order"), "0"));
        }
    });
    surveyMap.put("quests", dest);
    msg.put("data", surveyMap);
    Tools.encryptPrint(out, msg);
%>
